package com.dower.clothing.controller;

import java.util.List;
import com.dower.clothing.service.*;
import com.dower.common.annotation.Log;
import com.dower.common.config.Global;
import com.dower.common.core.controller.BaseController;
import com.dower.common.core.domain.AjaxResult;
import com.dower.common.core.page.TableDataInfo;
import com.dower.common.enums.BusinessType;
import com.dower.common.utils.file.FileUploadUtils;
import com.dower.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.dower.clothing.domain.ProductInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品信息Controller
 * 
 * @author dower
 * @date 2020-08-27
 */
@Slf4j
@Controller
@RequestMapping("/clothing/product_info")
public class ProductInfoController extends BaseController
{
    private String prefix = "clothing/product_info";

    @Autowired
    private IProductInfoService productInfoService;
    @Autowired
    private IClFnModelService clFnModelService;
    @Autowired
    private IClFnSchoolService clFnSchoolService;
    @Autowired
    private IClothingTypeService clothingTypeService;
    @Autowired
    private IProductCategoryService productCategoryService;

    @RequiresPermissions("clothing:product_info:view")
    @GetMapping()
    public String product_info()
    {
        return prefix + "/product_info";
    }

    /**
     * 查询商品信息列表
     */
    @RequiresPermissions("clothing:product_info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProductInfo productInfo)
    {
        startPage();
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        return getDataTable(list);
    }

    /**
     * 导出商品信息列表
     */
    @RequiresPermissions("clothing:product_info:export")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProductInfo productInfo)
    {
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        ExcelUtil<ProductInfo> util = new ExcelUtil<ProductInfo>(ProductInfo.class);
        return util.exportExcel(list, "product_info");
    }

    /**
     * 新增商品信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("clFnModelList", clFnModelService.selectClFnModelList(null));
        mmap.put("clFnSchoolList", clFnSchoolService.selectClFnSchoolList(null));
        mmap.put("clothingTypeList", clothingTypeService.selectClothingTypeList(null));
        mmap.put("productCategoryList", productCategoryService.selectProductCategoryList(null));
        return prefix + "/add";
    }

    /**
     * 新增保存商品信息
     */
    @RequiresPermissions("clothing:product_info:add")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProductInfo productInfo)
    {
        return toAjax(productInfoService.insertProductInfo(productInfo));
    }

    /**
     * 修改商品信息
     */
    @GetMapping("/edit/{productId}")
    public String edit(@PathVariable("productId") Integer productId, ModelMap mmap)
    {
        ProductInfo productInfo = productInfoService.selectProductInfoById(productId);
        mmap.put("productInfo", productInfo);
        mmap.put("clFnModelList", clFnModelService.selectClFnModelList(null));
        mmap.put("clFnSchoolList", clFnSchoolService.selectClFnSchoolList(null));
        mmap.put("clothingTypeList", clothingTypeService.selectClothingTypeList(null));
        mmap.put("productCategoryList", productCategoryService.selectProductCategoryList(null));
        return prefix + "/edit";
    }

    /**
     * 修改保存商品信息
     */
    @RequiresPermissions("clothing:product_info:edit")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProductInfo productInfo)
    {
        return toAjax(productInfoService.updateProductInfo(productInfo));
    }

    /**
     * 删除商品信息
     */
    @RequiresPermissions("clothing:product_info:remove")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(productInfoService.deleteProductInfoByIds(ids));
    }

    /**
     *
     * @param file 要上传的文件
     * @return
     */
    /*@RequestMapping("/imgUpload")
    public RetResult upload(@RequestParam("fileName") MultipartFile file, Map<String, Object> map){
        String localFilePath = resourceLocations.substring(5);
        return FileUtils.upload(file, localFilePath,localPath, file.getOriginalFilename());
    }*/
    /**
     * 上传产品图片
     */
    @RequestMapping("/imgUpload")
    @ResponseBody
    public AjaxResult updateAvatar(@RequestParam("productImg") MultipartFile file)
    {
        try
        {
            if (!file.isEmpty())
            {
                String avatar = FileUploadUtils.upload(Global.getUploadPath(), file);
                return AjaxResult.success("操作成功",avatar);
            }
            return error();
        }
        catch (Exception e)
        {
            log.error("修改头像失败！", e);
            return error(e.getMessage());
        }
    }
}
